VB Sample Code for modifying graphs

Private Sub mnuFormatGraphs_Click()
Dim result%, jobnum%, mainjob%, sectionN%, sectionCode%, GraphN%, GraphType%, TempText$
Dim GraphDataInfo As PEGraphDataInfo, GraphOptions As PEGraphOptions, GraphTextInfo As PEGraphTextInfo

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in FormatGraphs", jobnum%

' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in FormatGraphs", mainjob%
End If

' Define the size of structures
GraphDataInfo.StructSize = PE_SIZEOF_GRAPH_DATA_INFO
GraphOptions.StructSize = PE_SIZEOF_GRAPH_OPTIONS
GraphTextInfo.StructSize = PE_SIZEOF_GRAPH_TEXT_INFO

' Load the Sections form for displaying the selection of sections available
Load Sections
CenterForm Sample, Sections
Sections.Caption = "Graph Sections"
' Fill section list box with appropriate sections for report
result% = PEGetNSections(jobnum%)
For sectionN% = 0 To result% - 1
sectionCode% = PEGetSectionCode(jobnum%, sectionN%)
Select Case sectionCode%
Case Is >= 8000
TempText$ = "Report Footer"
Case Is >= 7000
TempText$ = "Page Footer"
Case Is >= 5000
TempText$ = "Group Footer"
Case Is >= 4000
TempText$ = "Detail"
Case Is >= 3000
TempText$ = "Group Header"
Case Is >= 2000
TempText$ = "Page Header"
Case Is >= 1000
TempText$ = "Report Header"
Case Else
TempText$ = "Unknown Section"
End Select
Sections!lstSections.AddItem TempText$
Sections!lstSections.ItemData(Sections!lstSections.NewIndex) = sectionCode%
Next sectionN%

' Start a loop for setting different section settings - exit by pressing ok on the sections form
Do While True
' Show Section selector form 1 ' Modally
Sections.Show 1 ' Modal
' Find which button was pressed to hide the Sections form
Select Case Sections.Tag
Case "Ok"
Exit Do
Case "Edit"
' Get the section code from the Section form
sectionCode% = Sections!lstSections.ItemData(Sections!lstSections.ListIndex)

' Get graph number
GraphN% = Val(InputBox("Enter graph number, first graph in section is zero.", "Graph Number", "0"))

' Load Graph edit form
Load Graphs
CenterForm Sample, Graphs

' Get Graph data
result% = PEGetGraphData(jobnum%, sectionCode%, GraphN%, GraphDataInfo)
ErrorTrap "GetGraphData in FormatGraphs", jobnum%
' Fill in graph data on form
Graphs!txtrowGroupN.Text = GraphDataInfo.RowGroupN
Graphs!txtcolGroupN.Text = GraphDataInfo.ColGroupN
Graphs!txtsummarizedFieldN.Text = GraphDataInfo.SummarizedFieldN
' Use a loop to find the matching graph direction description in the combo box
Graphs!cmbgraphDirection.ListIndex = -1
Do While Graphs!cmbgraphDirection.ListIndex <> Graphs!cmbgraphDirection.ListCount - 1
Graphs!cmbgraphDirection.ListIndex = Graphs!cmbgraphDirection.ListIndex + 1
If Graphs!cmbgraphDirection.ItemData(Graphs!cmbgraphDirection.ListIndex) = GraphDataInfo.GraphDirection Then Exit Do
Loop
If Graphs!cmbgraphDirection.ItemData(Graphs!cmbgraphDirection.ListIndex) <> GraphDataInfo.GraphDirection Then Graphs!cmbgraphDirection.ListIndex = - 1

' Get Graph Options
result% = crPEGetGraphOptions(jobnum%, sectionCode%, graphN%, graphOptions.GroupMaxValue, graphOptions.GroupMinValue, graphOptions.ShowDataValue, graphOptions.ShowGridLine, graphOptions.VerticalBars, graphOptions.ShowLegend, graphOptions.FontFaceName)
ErrorTrap "GetGraphOptions in FormatGraphs", jobnum%
' Fill in graph options on form
Graphs!txtgraphMaxValue.Text = GraphOptions.GraphMaxValue
Graphs!txtgraphMinValue.Text = GraphOptions.GraphMinValue
Graphs!chkshowDataValue.Value = IIf(GraphOptions.ShowDataValue, 1, 0)
Graphs!chkshowGridLine.Value = IIf(GraphOptions.ShowGridLine, 1, 0)
Graphs!chkverticalBars.Value = IIf(GraphOptions.VerticalBars, 1, 0)
Graphs!chkshowLegend.Value = IIf(GraphOptions.ShowLegend, 1, 0)
Graphs!txtfontFaceName.Text = GraphOptions.FontFaceName

' Get Graph Text
result% = PEGetGraphText(jobnum%, sectionCode%, GraphN%, GraphTextInfo)
ErrorTrap "GetGraphText in FormatGraphs", jobnum%
' Fill in graph texts on form
Graphs!txtgraphTitle.Text = GraphTextInfo.GraphTitle
Graphs!txtgraphSubTitle.Text = GraphTextInfo.GraphSubTitle
Graphs!txtgraphFootNote.Text = GraphTextInfo.GraphFootNote
Graphs!txtgraphGroupsTitle.Text = GraphTextInfo.GraphGroupsTitle
Graphs!txtgraphSeriesTitle.Text = GraphTextInfo.GraphSeriesTitle
Graphs!txtgraphXAxisTitle.Text = GraphTextInfo.GraphXAxisTitle
Graphs!txtgraphYAxisTitle.Text = GraphTextInfo.GraphYAxisTitle
Graphs!txtgraphZAxisTitle.Text = GraphTextInfo.GraphZAxisTitle

' Get Graph Type
result% = PEGetGraphType(jobnum%, sectionCode%, GraphN%, GraphType%)
ErrorTrap "GetGraphType in FormatGraphs", jobnum%
' Fill in graph type using loop to find combo box match on form
Graphs!cmbgraphType.ListIndex = -1
Do While Graphs!cmbgraphType.ListIndex <> Graphs!cmbgraphType.ListCount - 1
Graphs!cmbgraphType.ListIndex = Graphs!cmbgraphType.ListIndex + 1
If Graphs!cmbgraphType.ItemData(Graphs!cmbgraphType.ListIndex) = GraphType% Then Exit Do
Loop
If Graphs!cmbgraphType.ItemData(Graphs!cmbgraphType.ListIndex) <> GraphType% Then Graphs!cmbgraphType.ListIndex = -1


' Display Graph edit form
Graphs.Show 1 ' Modal
Select Case Graphs.Tag
Case "Ok"
' Set graph data
GraphDataInfo.RowGroupN = Val(Graphs!txtrowGroupN.Text)
GraphDataInfo.ColGroupN = Val(Graphs!txtcolGroupN.Text)
GraphDataInfo.SummarizedFieldN = Val(Graphs!txtsummarizedFieldN.Text)
GraphDataInfo.GraphDirection = Graphs!cmbgraphDirection.ItemData(Graphs!cmbgraphDirection.ListIndex)
result% = PESetGraphData(jobnum%, sectionCode%, GraphN%, GraphDataInfo)
ErrorTrap "SetGraphData in FormatGraphs", jobnum%

' Set graph options
GraphOptions.GraphMaxValue = Val(Graphs!txtgraphMaxValue.Text)
GraphOptions.GraphMinValue = Val(Graphs!txtgraphMinValue.Text)
GraphOptions.ShowDataValue = IIf(Graphs!chkshowDataValue.Value = 1, True, False)
GraphOptions.ShowGridLine = IIf(Graphs!chkshowGridLine.Value = 1, True, False)
GraphOptions.VerticalBars = IIf(Graphs!chkverticalBars.Value = 1, True, False)
GraphOptions.ShowLegend = IIf(Graphs!chkshowLegend.Value = 1, True, False)
GraphOptions.FontFaceName = Graphs!txtfontFaceName.Text
result% = crPESetGraphOptions(jobnum%, sectionCode%, graphN%, graphOptions.GraphMaxValue, graphOptions.GraphMinValue, graphOptions.ShowDataValue, graphOptions.ShowGridLine, graphOptions.VerticalBars, graphOptions.ShowLegend, graphOptions.FontFaceName)
ErrorTrap "SetGraphOptions in FormatGraphs", jobnum%

' Set graph text
GraphTextInfo.GraphTitle = Graphs!txtgraphTitle.Text & Chr$(0)
GraphTextInfo.GraphSubTitle = Graphs!txtgraphSubTitle.Text & Chr$(0)
GraphTextInfo.GraphFootNote = Graphs!txtgraphFootNote.Text & Chr$(0)
GraphTextInfo.GraphGroupsTitle = Graphs!txtgraphGroupsTitle.Text & Chr$(0)
GraphTextInfo.GraphSeriesTitle = Graphs!txtgraphSeriesTitle.Text & Chr$(0)
GraphTextInfo.GraphXAxisTitle = Graphs!txtgraphXAxisTitle.Text & Chr$(0)
GraphTextInfo.GraphYAxisTitle = Graphs!txtgraphYAxisTitle.Text & Chr$(0)
GraphTextInfo.GraphZAxisTitle = Graphs!txtgraphZAxisTitle.Text & Chr$(0)
result% = PESetGraphText(jobnum%, sectionCode%, GraphN%, GraphTextInfo)
ErrorTrap "SetGraphText in FormatGraphs", jobnum%

' Set graph type
GraphType% = Graphs!cmbgraphType.ItemData(Graphs!cmbgraphType.ListIndex)
result% = PESetGraphType(jobnum%, sectionCode%, GraphN%, GraphType%)
ErrorTrap "SetGraphType in FormatGraphs", jobnum%

Case "Cancel"
MsgBox "Cancel was pressed - no changes will be made to the graph settings", vbOKOnly + vbCritical, "Cancel Pressed"
Exit Do
End Select

Unload Graphs
End Select
Loop
Unload Sections

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-link preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Graph Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in FormatGraphs", jobnum%

result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in FormatGraphs", jobnum%

result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If

' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in FormatGraphs", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Graphs Complete!", vbOKOnly, "Operation Succeeded"


End Sub
ActiveX
Private Sub mnuFormatGraphs_Click()
Dim GraphN As Integer, graphDataN As Integer, graphOptionsN As Integer
Dim graphTextN As Integer, graphTypeN As Integer, TempText As String
Dim hwndPreviewWindow As Long

CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form

' Load the Sections form for displaying the selection of sections available
Load Sections
CenterForm Sample, Sections
Sections.Caption = "Graph Sections"

' Start a loop for setting different section settings - exit by pressing ok on the sections form
Do While True
' Show Section selector form modally
Sections.Show 1
' Find which button was pressed to hide the Sections form
Select Case Sections.Tag
Case "Ok"
Exit Do
Case "Edit"
' Get graph number
GraphN = Val(InputBox("Enter graph number, first graph in section is zero.", "Graph Number", "0"))

' Load Graph edit form
Load Graphs
CenterForm Sample, Graphs

' Display Graph edit form
Graphs.Show 1
Select Case Graphs.Tag
Case "Ok"
' Graph data
If Val(Graphs!txtrowGroupN.Text) <> 0 Then
' Create section name and graph number
TempText = Sections!cmbSectionType.Text & "." & Sections!txtGroup.Text & "." & Sections!txtSectionNumber.Text & ";" & Trim(Str$(GraphN))
' Set graph data
TempText = TempText & ";Group" & Graphs!txtrowGroupN.Text
TempText = TempText & ";Group" & Graphs!txtcolGroupN.Text
TempText = TempText & ";" & Graphs!txtsummarizedFieldN.Text
TempText = TempText & ";" & Graphs!cmbgraphDirection.List(Graphs!cmbgraphDirection.ListIndex)
CrystalReport1.GraphData(graphDataN) = TempText
graphDataN = graphDataN + 1
End If

' Set graph options
If Len(Trim(Graphs!txtfontFaceName.Text)) <> 0 Then
TempText = Sections!cmbSectionType.Text & "." & Sections!txtGroup.Text & "." & Sections!txtSectionNumber.Text & ";" & Trim(Str$(GraphN))
TempText = TempText & ";" & Graphs!txtfontFaceName.Text
If Graphs!chkverticalBars.Value = 1 Then TempText = TempText & ";V" Else TempText = TempText & ";H"
If Graphs!chkshowDataValue.Value = 1 Then TempText = TempText & ";T" Else TempText = TempText & ";F"
If Graphs!chkshowGridLine.Value = 1 Then TempText = TempText & ";T" Else TempText = TempText & ";F"
If Graphs!chkshowLegend.Value = 1 Then TempText = TempText & ";T" Else TempText = TempText & ";F"
TempText = TempText & ";" & Graphs!txtgraphMaxValue.Text
TempText = TempText & ";" & Graphs!txtgraphMinValue.Text
CrystalReport1.GraphOptions(graphOptionsN) = TempText
graphOptionsN = graphOptionsN + 1
End If

' Set graph text
If Len(Trim(Graphs!txtgraphTitle.Text)) <> 0 Then
TempText = Sections!cmbSectionType.Text & "." & Sections!txtGroup.Text & "." & Sections!txtSectionNumber.Text & ";" & Trim(Str$(GraphN))
TempText = TempText & ";" & Graphs!txtgraphTitle.Text
TempText = TempText & ";" & Graphs!txtgraphSubTitle.Text
TempText = TempText & ";" & Graphs!txtgraphFootNote.Text
TempText = TempText & ";" & Graphs!txtgraphGroupsTitle.Text
TempText = TempText & ";" & Graphs!txtgraphSeriesTitle.Text
TempText = TempText & ";" & Graphs!txtgraphXAxisTitle.Text
TempText = TempText & ";" & Graphs!txtgraphYAxisTitle.Text
TempText = TempText & ";" & Graphs!txtgraphZAxisTitle.Text
CrystalReport1.GraphText(graphTextN) = TempText
graphTextN = graphTextN + 1
End If

' Set graph type
If Len(Trim(Graphs!cmbgraphType.Text)) <> 0 Then
TempText = Sections!cmbSectionType.Text & "." & Sections!txtGroup.Text & "." & Sections!txtSectionNumber.Text & ";" & Trim(Str$(GraphN))
TempText = TempText & ";" & Graphs!cmbgraphType.Text
CrystalReport1.GraphType(graphTypeN) = TempText
graphTypeN = graphTypeN + 1
End If

Case "Cancel"
MsgBox "Cancel was pressed - no changes will be made to the graph settings", vbOKOnly + vbCritical, "Cancel Pressed"
Exit Do
End Select

Unload Graphs
End Select
Loop
Unload Sections

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "FormatGraphs"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If

' Close the report
CrystalReport1.ReportFileName = ""

MsgBox "Graphs Complete!", vbOKOnly, "Operation Completed"


End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com